package com.ruoyi.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.pojo.request.PageSearchParam;
import com.ruoyi.common.core.pojo.result.PageUtils;
import com.ruoyi.common.core.pojo.result.Pages;
import com.ruoyi.common.core.utils.CollectionUtils;
import com.ruoyi.user.mapper.UserDynamicMapper;
import com.ruoyi.user.model.pojo.entity.User;
import com.ruoyi.user.model.pojo.entity.UserDynamic;
import com.ruoyi.user.model.pojo.result.UserSearchInfo;
import com.ruoyi.user.service.IUserDynamicService;
import com.ruoyi.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户动态权限 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-08-06
 */
@Service
public class UserDynamicServiceImpl extends ServiceImpl<UserDynamicMapper, UserDynamic> implements IUserDynamicService {

    @Autowired
    private IUserService userService;


    @Override
    public Integer noLookTa(Long userId, Long aimUserId) {
        Long count = lambdaQuery().eq(UserDynamic::getUserId, userId)
                .eq(UserDynamic::getAimUserId, aimUserId)
                .count();
        if (count == 0) {
            UserDynamic userDynamic = new UserDynamic();
            userDynamic.setUserId(userId);
            userDynamic.setAimUserId(aimUserId);
            save(userDynamic);
            return 1;
        } else {
            remove(new QueryWrapper<UserDynamic>().lambda().eq(UserDynamic::getUserId, userId).eq(UserDynamic::getAimUserId, aimUserId));
            return 0;
        }
    }

    @Override
    public Integer getNoLookTa(Long userId, Long aimUserId) {
        Long count = lambdaQuery()
                .eq(UserDynamic::getUserId, userId)
                .eq(UserDynamic::getAimUserId, aimUserId)
                .count();
        if (count > 0) return 1;
        return 0;
    }

    @Override
    public Integer noLookMy(Long userId, Long aimUserId) {
        Long count = lambdaQuery()
                .eq(UserDynamic::getAimUserId, userId)
                .eq(UserDynamic::getUserId, aimUserId)
                .count();
        if (count == 0) {
            UserDynamic userDynamic = new UserDynamic();
            userDynamic.setUserId(aimUserId);
            userDynamic.setAimUserId(userId);
            save(userDynamic);
            return 1;
        } else {
            remove(new QueryWrapper<UserDynamic>().lambda().eq(UserDynamic::getUserId, aimUserId).eq(UserDynamic::getAimUserId, userId));
            return 0;
        }
    }

    @Override
    public Integer getNoLookMy(Long userId, Long aimUserId) {
        Long count = lambdaQuery()
                .eq(UserDynamic::getAimUserId, userId)
                .eq(UserDynamic::getUserId, aimUserId)
                .count();
        if (count > 0) return 1;
        return 0;
    }

    @Override
    public Pages<UserSearchInfo> noLookTaList(PageSearchParam param, Long userId) {
        Page<User> userPage = PageUtils.initPage(param, User.class);
        Set<Long> collect = lambdaQuery()
                .eq(UserDynamic::getUserId, userId)
                .list().stream().map(UserDynamic::getAimUserId).collect(Collectors.toSet());
        if (collect.isEmpty()) return PageUtils.pages(userPage, CollectionUtils.emptyList());
        Page<User> page = userService.lambdaQuery()
                .ne(User::getId, userId)
                .and(!StringUtils.isEmpty(param.getSearch()),
                        wapper -> {
                            wapper.like(User::getNickName, param.getSearch())
                                    .or()
                                    .like(User::getPhone, param.getSearch());
                        }
                )
                .in(!collect.isEmpty(), User::getId, collect)
                .page(userPage);
        List<UserSearchInfo> userList = userService.getUserList(page.getRecords(), userId);
        return PageUtils.pages(page, userList);
    }

    @Override
    public Pages<UserSearchInfo> noLookMyList(PageSearchParam param, Long userId) {
        Page<User> userPage = PageUtils.initPage(param, User.class);
        Set<Long> collect = lambdaQuery()
                .eq(UserDynamic::getAimUserId, userId)
                .list().stream().map(UserDynamic::getUserId).collect(Collectors.toSet());
        if (collect.isEmpty()) return PageUtils.pages(userPage, CollectionUtils.emptyList());
        Page<User> page = userService.lambdaQuery()
                .ne(User::getId, userId)
                .in(!collect.isEmpty(), User::getId, collect)
                .page(userPage);
        List<UserSearchInfo> userList = userService.getUserList(page.getRecords(), userId);
        return PageUtils.pages(page, userList);
    }

    @Override
    public Set<Long> getNoDynamicUserIds(Long userId) {
        return lambdaQuery().eq(UserDynamic::getUserId, userId).list().stream().map(UserDynamic::getAimUserId).collect(Collectors.toSet());
    }
}
